package View;

import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JButton;

import Entity.Cities;
import Entity.Customer;

import java.awt.Font;
import java.io.Serializable;

/** this class provide the GUI window in order to update customers information */
public class UpdateCustomerDeatilsView extends JFrame  implements Serializable {

	/**required  fields and buttons */
	private static final long serialVersionUID = 42L;
	JTextField IdField;
	JTextField firstNameField;
	JComboBox CityComboBox;
	JLabel lblPhoneNumber;
	JTable table;
	JTextField PhoneField;
	JButton UpdateBtn;
	JButton BackBtn;
	JTextField lastNameField;

	/** default and only constructor */
	public UpdateCustomerDeatilsView() {
		getContentPane().setLayout(null);
		setResizable(false);

		this.setTitle("Update Customer");
		this.setSize(436, 365);


		JLabel lblCustumerId = new JLabel("CustomerID");
		lblCustumerId.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCustumerId.setBounds(48, 39, 109, 20);
		getContentPane().add(lblCustumerId);

		IdField = new JTextField();
		IdField.setBounds(214, 40, 135, 20);
		getContentPane().add(IdField);
		IdField.setColumns(10);

		JLabel lblName = new JLabel("First Name");
		lblName.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblName.setBounds(48, 73, 79, 15);
		getContentPane().add(lblName);

		firstNameField = new JTextField();
		firstNameField.setColumns(10);
		firstNameField.setBounds(214, 71, 135, 20);
		getContentPane().add(firstNameField);

		JLabel lblAg = new JLabel("City");
		lblAg.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblAg.setBounds(48, 152, 79, 17);
		getContentPane().add(lblAg);

		int x=2016-1900;
		String[] listOfElements = new String[x];

		for(int i=0;i<x;i++)
			listOfElements[i] = Integer.toString(i+1900);



		lblPhoneNumber = new JLabel("Phone Number");
		lblPhoneNumber.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblPhoneNumber.setBounds(48, 197, 109, 16);
		getContentPane().add(lblPhoneNumber);

		table = new JTable();
		table.setBounds(222, 239, 141, -41);
		getContentPane().add(table);

		PhoneField = new JTextField();
		PhoneField.setBounds(214, 196, 135, 20);
		getContentPane().add(PhoneField);
		PhoneField.setColumns(10);

		UpdateBtn = new JButton("Update");
		UpdateBtn.setFont(new Font("Tahoma", Font.BOLD, 15));
		UpdateBtn.setBounds(239, 270, 135, 28);
		getContentPane().add(UpdateBtn);

		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblLastName.setBounds(45, 111, 82, 14);
		getContentPane().add(lblLastName);

		lastNameField = new JTextField();
		lastNameField.setBounds(214, 109, 135, 20);
		getContentPane().add(lastNameField);
		lastNameField.setColumns(10);

		BackBtn = new JButton("Back");
		BackBtn.setFont(new Font("Tahoma", Font.BOLD, 15));
		BackBtn.setBounds(48, 270, 135, 28);
		getContentPane().add(BackBtn);


		CityComboBox = new JComboBox(Cities.getCitiesStringArr());
		CityComboBox.setBounds(214, 151, 135, 20);
		CityComboBox.addActionListener(CityComboBox);
		getContentPane().add(CityComboBox);


	}


	/**show and hide window functions */
	public void showWindow(){
		setVisible(true);
	}
	public void hideWindow(){
		setVisible(false);
	}

	/**
	 * @return button UpdateBtn
	 */
	public JButton getUpdateBtn()
	{
		return this.UpdateBtn;
	}
	/**
	 * 
	 * @return button BackBtn
	 */
	public JButton getBackBtn()
	{
		return this.BackBtn;
	}
	/**
	 * 
	 * @return text field firstNameField
	 */
	public String getFirstName(){
		return firstNameField.getText();
	}

	/**
	 * @return LastNameField Text
	 */
	public String getlastName(){
		return lastNameField.getText();
	}



	/**
	 * @return city Text 
	 */
	public String getCity(){
		return (String) CityComboBox.getSelectedItem();
	}

	/**
	 * @return PhoneField Text
	 */
	public String getPhone(){
		return PhoneField.getText();
	}

	/**
	 * @return id Text 
	 */
	public String getID() {
		return IdField.getText();
	}


	/**
	 * Function which clear first name field
	 */
	public void clearFirstName(){
		firstNameField.setText("");
	}

	/**
	 * Function which clear last name field
	 */
	public void clearLastName(){
		lastNameField.setText("");
	}

	/**
	 *Function which clear city field
	 */
	public void clearCity(){
		CityComboBox.setSelectedItem(" ");
	}

	/**
	 * Function which clear age field
	 */
	public void clearPhone(){
		PhoneField.setText("");
	}
	/**
	 * Function which clear age field
	 */
	public void clearFields(){
		PhoneField.setText("");
		lastNameField.setText("");
		firstNameField.setText("");
		IdField.setText("");
		CityComboBox.setSelectedItem(" ");


	}
	/**
	 * @return id Text
	 */
	public void  setID(){
		IdField.setText("");
	}

	/**
	 *add listeners to the buttons
	 */
	public void addActionListenerOur(ActionListener actionListener) {
		UpdateBtn.addActionListener(actionListener);
		BackBtn.addActionListener(actionListener);
	}


	/**
	 * set fields function
	 */
	public void setDetails(Object data) {
		Customer customer = (Customer) data;
		IdField.setText(customer.getCustomerID());
		IdField.setEditable(false);
		firstNameField.setText(customer.getFirstName());
		lastNameField.setText(customer.getLastName());
		PhoneField.setText(customer.getPhoneNumber());
		CityComboBox.setSelectedItem(customer.getCity());
	}

}
